//
//  AddSizeViewController.m
//  quickBatchManager
//
//  Created by 王定波 on 15/9/22.
//  Copyright © 2015年 jack. All rights reserved.
//

#import "ClearPriceViewController.h"

@interface ClearPriceViewController ()
{
    NSMutableArray *buttonList;
    PMCalendarController *pmCC;
    NSDate *nowdate;
}
@end

@implementation ClearPriceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)initUI
{
    UIImage *image = _bgImage.image;
    image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:floorf(image.size.height/2)];
    _bgImage.image=image;
    self.view.backgroundColor=[UIColor clearColor];
    
    _noteField.layer.borderColor=[UIColor colorWithWhite:0.85 alpha:1].CGColor;
    _noteField.layer.borderWidth=0.5;
    _noteField.layer.cornerRadius=5;
    _noteField.clipsToBounds=YES;
    _noteField.backgroundColor=[UIColor whiteColor];
    
    
    _contentField.layer.borderColor=[UIColor colorWithWhite:0.85 alpha:1].CGColor;
    _contentField.layer.borderWidth=0.5;
    _contentField.layer.cornerRadius=5;
    _contentField.clipsToBounds=YES;
    _contentField.backgroundColor=[UIColor whiteColor];
    
    
    _dateField.delegate=self;
    _dateField.tag=1;
    _dateField.inputView=[[UIView alloc] init];
    _dateField.inputAccessoryView=[[UIView alloc] init];
    _dateField.text=[nowdate dateStringWithFormat:YYYYMMDD];

    
    [_submitBtn bk_whenTapped:^{
        
    
        ClearPriceModel *model=[[ClearPriceModel alloc] init];
        model.clearDate=[nowdate dateStringWithFormat:YYYYMMDDHHMMSS];
        model.content=_contentField.text;
        
         if(_accountType==AccountTypeSupplier)
         {
             model.supplierId=_supplier.supplierId;
             model.price=[AppService getAmountByUserIdAndDateAndType:_supplier.supplierId andDate:[nowdate dateStringWithFormat:YYYYMMDDHHMMSS] andType:_accountType];
         }
         else {
             model.clientId=_client.clientId;
             model.price=[AppService getAmountByUserIdAndDateAndType:_client.clientId andDate:[nowdate dateStringWithFormat:YYYYMMDDHHMMSS] andType:_accountType];
         }
        model.remark=_noteField.text;
        
        [ClearPriceModel insertToDB:model];
        
        
        if([_delegate respondsToSelector:@selector(didChange:andData:)])
        {
            [_delegate didChange:ChangeTypeDefult andData:nil];
        }

        
        
        [self showAlert:@"添加成功" handler:^{
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
    }];
    
    
    [_closeBtn bk_whenTapped:^{
           [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
}


-(void) initData
{
    nowdate=[NSDate date];
    buttonList=[[NSMutableArray alloc] init];
    [self reloadData];
}


-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField.tag==1 && [pmCC isCalendarVisible])
    {
        [pmCC dismissCalendarAnimated:NO];
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if(textField.tag==1)
    {
        pmCC = [[PMCalendarController alloc] initWithThemeName:@"default"];
        pmCC.delegate = self;
        pmCC.mondayFirstDayOfWeek = NO;
        if ([pmCC isCalendarVisible])
        {
            [pmCC dismissCalendarAnimated:NO];
        }
         pmCC.period = [PMPeriod oneDayPeriodWithDate:nowdate];
        [pmCC presentCalendarFromView:_dateField permittedArrowDirections:PMCalendarArrowDirectionUp animated:YES];
    }
}

#pragma PMCalendarControllerDelegate
- (void)calendarControllerDidDismissCalendar:(PMCalendarController *)calendarController
{
    [_dateField resignFirstResponder];
}

- (void)calendarController:(PMCalendarController *)calendarController didChangePeriod:(PMPeriod *)newPeriod;
{
    nowdate=newPeriod.startDate;
    _dateField.text=[NSString stringWithFormat:@"%@",[newPeriod.startDate dateStringWithFormat:YYYYMMDD]];
    [self reloadData];
}


-(void) reloadData
{
    if(_accountType==AccountTypeSupplier)
        _contentField.text=[NSString stringWithFormat:@"截至：%@ 欠款：%0.2f 供应商：%@",[nowdate dateStringWithFormat:YYYYMMDD],[AppService getAmountByUserIdAndDateAndType:_supplier.supplierId andDate:[nowdate dateStringWithFormat:YYYYMMDDHHMMSS] andType:_accountType],_supplier.contactName];
    else  _contentField.text=[NSString stringWithFormat:@"截至 %@ %@ 欠款：%0.2f",[nowdate dateStringWithFormat:YYYYMMDD],_client.clientName,[AppService getAmountByUserIdAndDateAndType:_client.clientId andDate:[nowdate dateStringWithFormat:YYYYMMDDHHMMSS] andType:_accountType]];
}

@end
